package com.qmgyl.marketshop.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.qmfresh.service.gateway.dto.AdminUserLogin;
import com.qmfresh.service.shop.facade.dto.LoginBean;
import com.qmgyl.marketshop.api.ServiceApi;

public class LoginHandlerInterceptor extends HandlerInterceptorAdapter{

	@Autowired
	private ServiceApi serviceApi;
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String token = (String)request.getSession().getAttribute("token");
		if(StringUtils.isBlank(token)) {
			response.sendRedirect("/login");
			return false;
		}
		else {
			//调服务端接口判断token有效性
			LoginBean lb = new LoginBean();
			lb.setToken(token);
			AdminUserLogin user = serviceApi.ssoValidate(lb);
			if(user == null) {
				response.sendRedirect("/login");
				return false;
			}
		}
		return super.preHandle(request, response, handler);
	}

}
